package com.dns.customers.controller;

import com.dns.customers.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 用户登录
     * @param userName 账号
     * @param passWord 密码
     * @return
     */
    @RequestMapping(value = "/login",produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String login(HttpServletRequest req, HttpServletResponse res,String userName, String passWord){
        return loginService.login(req,res,userName,passWord);
    }
    /**
     * 退出
     */
    @RequestMapping(value = "/logout",produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String logout(HttpServletRequest req,HttpServletResponse res) {
        String result = null;
        try {
            req.getSession().invalidate();
            result =  "{\"result\":{\"errorMsg\":\"退出成功\"}}";
        }catch (Exception e){
            result =  "{\"result\":{\"errorMsg\":\"退出异常\",\"errorCode\":\"1111\"}}";
        }
        return result;
    }
}
